package com.algorithm.learning.base.二叉树;

/**
 * @program: algorithm-learning
 * @description:
 * @author: YuKai Fan
 * @create: 2024/8/23 22:07
 **/
public class 二叉树 {

    public static class Node {
        private Node left;
        private Node right;
        private int value;

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * 先序遍历
     * @param head
     */
    public static void pre(Node head) {
        if (head == null) {
            return;
        }
        System.out.println(head.value);
        pre(head.left);
        pre(head.right);
    }

    /**
     * 中序遍历
     * @param head
     */
    public static void in(Node head) {
        if (head == null) {
            return;
        }
        in(head.left);
        System.out.println(head.value);
        in(head.right);
    }

    /**
     * 后序遍历
     * @param head
     */
    public static void pos(Node head) {
        if (head == null) {
            return;
        }

        pos(head.left);
        pos(head.right);

        System.out.println(head.value);
    }

    /**
     * 递归序
     * @param head
     */
    public static void f(Node head) {
        if (head == null) {
            return;
        }
        // 1. 如果这是打印位置，那就是先序遍历 System.out.println(head.value);
        f(head.left);
        // 2. 如果这是打印位置，那就是中序遍历 System.out.println(head.value);
        f(head.right);
        // 3. 如果这是打印位置，那就是后序遍历 System.out.println(head.value);
    }
}
